home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / smalltlk.zip / PRELUDE / KCOLLECT.ST < prev    next >
Text File  |  1987-06-17  |  3KB  |  91 lines

  1. Class KeyedCollection :Collection
  2. [
  3.     add: anElement
  4.         ^ self error: 'Must add with explicit key'
  5. |
  6.     addAll: aCollection
  7.                 aCollection binaryDo: [:x :y | self at: x put: y].
  8.                 ^ aCollection
  9. |
  10.     asDictionary            | newCollection |
  11.         newCollection <- Dictionary new.
  12.         self binaryDo:
  13.             [:key :val | newCollection at: key put: val].
  14.         ^ newCollection
  15. |
  16.     at: key
  17.                 ^ self at: key ifAbsent:
  18.                    [self error:
  19.                          (key printString , ': association not found').
  20.                     ^ key]
  21. |
  22.     atAll: aCollection put: anObject
  23.         aCollection do: [:x | self at: x put: anObject]
  24. |
  25.     binaryDo: aBlock                | item |
  26.         self do: [:x | aBlock value: self currentKey
  27.                     value: x ].
  28.                 ^ nil
  29. |
  30.     coerce: aCollection    | newobj |
  31.         newobj <- self class new.
  32.         aCollection binaryDo: [:x :y | newobj at: x put: y].
  33.         ^ newobj
  34. |
  35.     collect: aBlock
  36.         ^ self coerce:
  37.              (self inject: Dictionary new
  38.                            into: [:x :y | x at: self currentKey
  39.                         put: (aBlock value: y) . x ] )
  40. |
  41.     includesKey: key
  42.                 self at: key ifAbsent: [^ false].
  43.                 ^ true
  44. |
  45.     indexOf: anElement
  46.         ^ self indexOf: anElement
  47.         ifAbsent: [self error: 'indexOf element not found']
  48. |
  49.     indexOf: anElement ifAbsent: exceptionBlock
  50.                 self do: [:x | (x = anElement)
  51.                     ifTrue: [ ^ self currentKey ]].
  52.                  ^ exceptionBlock value
  53. |
  54.     keys                             | newset |
  55.                 newset <- Set new.
  56.                 self keysDo: [:x | newset add: x].
  57.                 ^ newset
  58. |
  59.     keysDo: aBlock
  60.                 ^ self do: [ :x | aBlock value: self currentKey ]
  61. |
  62.     keysSelect: aBlock
  63.         ^ self coerce:
  64.              (self inject: Dictionary new
  65.                            into: [:x :y | (aBlock value: y currentKey)
  66.                                            ifTrue: [x at: self currentKey
  67.                                                       put: y]. x ] )
  68. |
  69.     remove: anElement
  70.         ^ self error: 'object must be removed with explicit key'
  71. |
  72.     removeKey: key
  73.                 ^ self removeKey: key ifAbsent:
  74.                    [self error: 'no element associated with key'. ^ key]
  75. |
  76.     removeKey: key ifAbsent: exceptionBlock
  77.         ^ self error: 'subclass should implement RemoveKey:ifAbsent:'
  78. |
  79.     select: aBlock
  80.         ^ self coerce:
  81.              (self inject: Dictionary new
  82.                            into: [:x :y | (aBlock value: y)
  83.                                            ifTrue: [x at: self currentKey
  84.                                                       put: y]. x ] )
  85. |
  86.     values                           | newbag |
  87.                 newbag <- Bag new.
  88.                 self do: [:x | newbag add: x].
  89.                 ^ newbag
  90. ]
  91.